﻿<%@ codepage=65001%><% 'UTF-8%>
<% '会员卡管理%>
<!--#include file="../Include/Common.All.asp"-->
<!--#include file="../Include/Conn.asp"-->
<% call OpenConn%>

<!--#include file="../Include/Common.Security.asp"-->
<!--#include file="../Include/MD5.asp"-->
<!--#include file="../Include/JSON_2.0.2.asp"-->
<!--#include file="../Include/JSON_UTIL_0.1.1.asp"-->



<%

	'定义json对象
	Dim jsonObj,jsonObj_user
	Set jsonObj = jsObject()
	Set jsonObj_user = jsObject()
	UserID=ReplaceBadChar(Trim(Session("UserID")))
	
	dim act 
  act=request("act")
 	Dim rsObj,strSql
	''查询信息列表
	if "search"=act then
		key=ReplaceBadChar(Trim(request.Form("key")))
				
		jsonObj("rslist") = ""
		''添加用户信息
		jsonObj_user("UserID") = UserID 
		jsonObj_user("UserName") =ReplaceBadChar(Trim(Session("UserName")))
		Set jsonObj("customer") = jsonObj_user
	
	 
		''处理key值和value的对应关系 eg:"cardtype":[{"key":"0","value":"\u5931\u6548"},{"key":"1","value":"\u751F\u6548"}]
		Dim jsonObj_map 
		Set jsonObj_map = jsArray()
		
		set jsonObj_map(0) = jsObject()
		jsonObj_map(0)("key") = "0"
		jsonObj_map(0)("value") = "未开卡"
		set jsonObj_map(1) = jsObject()
		jsonObj_map(1) ("key") = "1"
		jsonObj_map(1) ("value") = "生效"
		set jsonObj_map(2) = jsObject()
		jsonObj_map(2) ("key") = "2"
		jsonObj_map(2) ("value") = "锁定"
		set jsonObj_map(3) = jsObject()
		jsonObj_map(3) ("key") = "3"
		jsonObj_map(3) ("value") = "作废"
	
	
		Set jsonObj("cardtype") =  jsonObj_map
		''处理key值和value的对应关系--end
	
		If ("" = UserID ) Then UserID = "-999hasd9huoad9e993jwoi982niuh9wncdisj99"
	 
		''添加查询结果信息
		''Dim rsObj,strSql
		strSql = " SELECT HY_KH.Hycode, HY_KH.cardcode, KH_XX.yzm, KH_XX.batch, KH_XX.remarks   "
		strSql=strSql & " ,KH_XX.batchcode, KH_XX.creattime, HY_KH.id, HY_KH.zt , HY_KH.flag"
		strSql=strSql & " FROM HY_KH INNER JOIN KH_XX ON HY_KH.cardcode = KH_XX.cardcode "
		strSql=strSql & " WHERE 1=2 "
	  strSql=strSql & " or (HY_KH.Hycode ='"&UserID&"' "
		If (key <> "") Then strSql=strSql & " and HY_KH.cardcode like '%" & key & "%'  "
		strSql=strSql & " ) "
		strSql=strSql & " ORDER BY HY_KH.zt "
		Set jsonObj("rslist") = QueryToJSON(Conn, strSql)
		
 elseif "add"=act then
''添加信息
			cardid=ReplaceBadChar(Trim(request.Form("cardid")))
			card_Validate=ReplaceBadChar(Trim(request.Form("card_Validate")))
			If (cardid <> "" and card_Validate <> "" and UserID <> "" ) Then
				dim cardid_t
				strSql = "select count(cardcode)as 'aaaaa' from KH_XX where cardcode='"&cardid&"' and yzm='"&card_Validate&"' and zt=0"
				cardid_t = Conn.Execute(strSql)(0)
				if (0 = PE_CLng(cardid_t)) Then
						jsonObj("msg") = "添加失败，卡名和验证码错误！" 
						jsonObj("success") = 0'"false"
				else
						''Dim rsObj,strSql
						set rsObj=Server.CreateObject("ADODB.Recordset")
						strSql = "Select  * from HY_KH where cardcode='"&cardid&"' and Hycode ='"&UserID&"'"
						rsObj.Open strSql, Conn, 1, 3
						If rsObj.BOF And rsObj.EOF Then rsObj.AddNew '如果没记录新增，有记录update					
						rsObj("Hycode") = UserID
						rsObj("cardcode") = cardid
						rsObj("zt") = 1
						rsObj("flag") = 1''状态标示 缺省卡2、发卡为1
						rsObj.Update
						rsObj.close
						Set rsObj = Nothing
						''''改变会员卡的状态
						strSql = "Update KH_XX set zt=1  where cardcode='"&cardid&"'"
						Conn.Execute(strSql)
					 
						jsonObj("msg") = "添加成功，正在跳转..."
						jsonObj("success") = 1'"success"
						jsonObj("url") = "mycard.asp"
				end if
			else
					jsonObj("msg") = "添加失败，请正确填写卡名和验证码！"
					jsonObj("success") = 0'"false"
			End If  
 elseif "delete"=act then
''删除记录
		cardid=ReplaceBadChar(Trim(request.Form("id"))) 
    Dim  RowCount,RowCounthh 
    RowCount = 0
    strSql="SELECT COUNT(id) AS Expr1 FROM HYKXH WHERE (Cardcode = '"&cardid&"')"
    RowCount = Conn.Execute(strSql)(0)
    if (0<RowCount) then 
    	jsonObj("success") = 0 'false
      jsonObj("msg") = "该卡号存在消费记录，卡号删除失败。"
    else 
	    RowCount = 0
	    RowCounthh=0
	    If (cardid <> "" and UserID <> "" ) Then 
	    	strSql="Update HY_KH SET zt = 3 where cardcode='"&cardid&"' and Hycode ='"&UserID&"'"
	    	Conn.Execute (strSql), RowCount ''卡号用户关联关系 状态改为作废  	
	    	strSql="Update KH_XX SET zt = 3 where cardcode='"&cardid&"' "
	    	Conn.Execute (strSql), RowCounthh	  ''卡号作废  	
	    End If
	    If (2 = RowCounthh + RowCount) Then
	        jsonObj("msg") = "删除成功，正在跳转..."'
					jsonObj("success") = 1'"success"
					jsonObj("url") = "mycard.asp"
	    Else
	        jsonObj("success") = 0 'false
	        jsonObj("msg") = "记录删除失败。"
	    End If
    end if
  

	end If''''删除记录
 

	'输出json对象
	jsonObj.Flush
	''Response.Write strSql&"-----"
 
	call CloseConn

''{
''    rslist : [ { name: "mac", desc: "computer",     
''                   price: 1000, quantity: 100, alert:null },
''                 { name: "ipod", desc: "music player", 
''                   price:  200, quantity: 200, alert:"on sale now!" },
''                 { name: "cinema display", desc: "screen",       
''                   price:  800, quantity: 300, alert:"best deal!" } ],
''    customer : { first: "John", last: "Public", level: "gold" }
''}

%>